package goods

import (
	"context"
	"enjoy-shop/formdata"
	"gitee.com/enjoy_5/goods-rpc/client"
	"gitee.com/enjoy_5/goods-rpc/proto/goods"
)

func GetGoodsInfo(req *formdata.GoodsInfo) (*goods.GoodsInfo, error) {
	info, err := client.GetGoodsInfo(context.Background(), req.Id)
	if err != nil {
		return nil, err
	}
	return &goods.GoodsInfo{
		Id:            info.Id,
		Title:         info.Title,
		Point:         info.Point,
		Description:   info.Description,
		Img:           info.Img,
		PropertyInfos: info.PropertyInfos,
	}, nil
}

func GetSkuInfoByPropertyValueIDs(req *formdata.GoodsSku) (*goods.SkuInfo, error) {
	ds, err := client.GetSkuInfoByPropertyValueIDs(context.Background(), req.ProValueId1, req.ProValueId2, req.ProValueId3)
	if err != nil {
		return nil, err
	}
	return &goods.SkuInfo{
		Price: ds.Price,
		Stock: ds.Stock,
	}, nil
}

func SearchGoodsByTitle(req *formdata.GoodsInfo) ([]*goods.GoodsInfo, error) {
	byTitle, err := client.SearchGoodsByTitle(context.Background(), req.Title, req.Page, req.PageSize)
	if err != nil {
		return nil, err
	}
	return byTitle, nil
}

func GoodList(req *formdata.GoodList) ([]*goods.GoodListInfo, error) {
	list, err := client.GoodsList(context.Background(), req.Page, req.PageSize, req.TypesID, req.Title)
	if err != nil {
		return nil, err
	}
	return list, nil
}
